package cn.iocoder.yudao.module.train.dal.mysql.teacher;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.train.controller.admin.coursecommodity.vo.CourseCommodityPageReqVO;
import cn.iocoder.yudao.module.train.controller.admin.student.vo.StudentPageReqVO;
import cn.iocoder.yudao.module.train.dal.dataobject.WxUser.WxUserDO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseCommodityDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.StudentUserDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.TeacherUserDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.course.CourseDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecommodity.CourseCommodityDO;
import cn.iocoder.yudao.module.train.dal.dataobject.staff.StaffDO;
import cn.iocoder.yudao.module.train.dal.dataobject.student.StudentDO;
import cn.iocoder.yudao.module.train.dal.dataobject.teacher.TeacherDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.train.controller.admin.teacher.vo.*;

/**
 * 教师用户 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface TeacherMapper extends BaseMapperX<TeacherDO> {

//    default PageResult<TeacherDO> selectPage(TeacherPageReqVO reqVO) {
//        return selectPage(reqVO, new LambdaQueryWrapperX<TeacherDO>()
//                .eqIfPresent(TeacherDO::getUserId, reqVO.getUserId())
//                .likeIfPresent(TeacherDO::getTeacherName, reqVO.getTeacherName())
//                .eqIfPresent(TeacherDO::getTeacherAge, reqVO.getTeacherAge())
//                .eqIfPresent(TeacherDO::getTeacherSex, reqVO.getTeacherSex())
//                .eqIfPresent(TeacherDO::getTeacherIntro, reqVO.getTeacherIntro())
//                .betweenIfPresent(TeacherDO::getCreateTime, reqVO.getCreateTime())
//                .orderByDesc(TeacherDO::getId));
//    }


    default PageResult<TeacherUserDTO> selectPage(TeacherPageReqVO reqVO) {
        // 创建MyBatis-Plus分页对象
        Page<TeacherUserDTO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());

        // 创建连接查询条件
        MPJLambdaWrapper<TeacherDO> wrapper = new MPJLambdaWrapper<TeacherDO>()
                .selectAll(TeacherDO.class)
                .select(WxUserDO::getUName)
                .selectAs(WxUserDO::getId, TeacherUserDTO::getUserId)
                .select(WxUserDO::getUOpenId)
                .select(WxUserDO::getUAvatar)
                .select(WxUserDO::getUStatus)
                .leftJoin(WxUserDO.class, on -> on
                                .eq(WxUserDO::getId, TeacherDO::getUserId)
                        // 移除 u_status 条件，改到 where 中处理
                );

        // 添加过滤条件
        if (reqVO.getTeacherName() != null) {
            wrapper.like(TeacherDO::getTeacherName, reqVO.getTeacherName());
        }
        if (reqVO.getTeacherPhoneNumber() != null) {
            wrapper.like(TeacherDO::getTeacherPhoneNumber, reqVO.getTeacherPhoneNumber());
        }
        if (reqVO.getTeacherAge() != null) {
            wrapper.eq(TeacherDO::getTeacherAge, reqVO.getTeacherAge());
        }
        if (reqVO.getTeacherSex() != null) {
            wrapper.eq(TeacherDO::getTeacherSex, reqVO.getTeacherSex());
        }
        if (reqVO.getCreateTime() != null && reqVO.getCreateTime().length == 2) {
            wrapper.between(TeacherDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        if (reqVO.getTeacherIntro() != null) {
            wrapper.like(TeacherDO::getTeacherIntro, reqVO.getTeacherIntro());
        }
        if (reqVO.getTeacherBirthdate() != null && reqVO.getTeacherBirthdate().length == 2) {
            wrapper.between(TeacherDO::getTeacherBirthdate, reqVO.getTeacherBirthdate()[0], reqVO.getTeacherBirthdate()[1]);
        }

        // 处理 u_status 条件 - 要考虑 wx_user 为 null 的情况
        if (reqVO.getUStatus() != null) {
            wrapper.and(w -> w
                    .isNull(WxUserDO::getId)  // wx_user 不存在的情况
                    .or()
                    .eq(WxUserDO::getUStatus, reqVO.getUStatus())  // wx_user 存在且 status 匹配
            );
        }

        // 处理 u_role 条件 - 要考虑 wx_user 为 null 的情况
        wrapper.and(w -> w
                .isNull(WxUserDO::getId)  // wx_user 不存在的情况
                .or()
                .eq(WxUserDO::getURole, 1)  // wx_user 存在且 role = 1
        );

        wrapper.orderByDesc(TeacherDO::getId);

        // 执行连接查询并分页
        IPage<TeacherUserDTO> resultPage = selectJoinPage(page, TeacherUserDTO.class, wrapper);

        // 转换为框架的PageResult
        return new PageResult<>(resultPage.getRecords(), resultPage.getTotal());
    }


    default TeacherUserDTO selectTeacherUser(Long id) {
        return selectJoinOne(TeacherUserDTO.class,
                new MPJLambdaWrapper<TeacherDO>()
                        .selectAll(TeacherDO.class)
                        .leftJoin(WxUserDO.class, WxUserDO::getId, TeacherDO::getUserId)

                        // 关联表的列全部显式别名到 DTO 属性名（用“字符串别名”避免大小写问题）
                        .selectAs(WxUserDO::getId,           "uId")
                        .select(WxUserDO::getUOpenId)
                        .select(WxUserDO::getUName)
                        .select(WxUserDO::getUAvatar)
                        .select(WxUserDO::getUStatus)  // 关键：别名必须是 uStatus（小写u）

                        .eq(TeacherDO::getId, id)
        );
    }

    default List<TeacherUserDTO> selectList(TeacherPageReqVO reqVO) {
        // 创建连接查询条件
        MPJLambdaWrapper<TeacherDO> wrapper = new MPJLambdaWrapper<TeacherDO>()
                .selectAll(TeacherDO.class)
                .select(WxUserDO::getUName)
                .selectAs(WxUserDO::getId, TeacherUserDTO::getUserId)
                .select(WxUserDO::getUOpenId)
                .select(WxUserDO::getUAvatar)
                .select(WxUserDO::getUStatus)
                .leftJoin(WxUserDO.class, on -> on
                                .eq(WxUserDO::getId, TeacherDO::getUserId)
                        // 移除 u_status 条件，改到 where 中处理
                );

        // 添加过滤条件
        if (reqVO.getTeacherName() != null) {
            wrapper.like(TeacherDO::getTeacherName, reqVO.getTeacherName());
        }
        if (reqVO.getTeacherPhoneNumber() != null) {
            wrapper.like(TeacherDO::getTeacherPhoneNumber, reqVO.getTeacherPhoneNumber());
        }
        if (reqVO.getTeacherAge() != null) {
            wrapper.eq(TeacherDO::getTeacherAge, reqVO.getTeacherAge());
        }
        if (reqVO.getTeacherSex() != null) {
            wrapper.eq(TeacherDO::getTeacherSex, reqVO.getTeacherSex());
        }
        if (reqVO.getCreateTime() != null && reqVO.getCreateTime().length == 2) {
            wrapper.between(TeacherDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        if (reqVO.getTeacherIntro() != null) {
            wrapper.like(TeacherDO::getTeacherIntro, reqVO.getTeacherIntro());
        }
        if (reqVO.getTeacherBirthdate() != null && reqVO.getTeacherBirthdate().length == 2) {
            wrapper.between(TeacherDO::getTeacherBirthdate, reqVO.getTeacherBirthdate()[0], reqVO.getTeacherBirthdate()[1]);
        }

        // 处理 u_status 条件 - 要考虑 wx_user 为 null 的情况
        if (reqVO.getUStatus() != null) {
            wrapper.and(w -> w
                    .isNull(WxUserDO::getId)  // wx_user 不存在的情况
                    .or()
                    .eq(WxUserDO::getUStatus, reqVO.getUStatus())  // wx_user 存在且 status 匹配
            );
        }

        // 处理 u_role 条件 - 要考虑 wx_user 为 null 的情况
        wrapper.and(w -> w
                .isNull(WxUserDO::getId)  // wx_user 不存在的情况
                .or()
                .eq(WxUserDO::getURole, 1)  // wx_user 存在且 role = 1
        );

        wrapper.orderByDesc(TeacherDO::getId);
        // 转换为框架的PageResult
        return selectJoinList(TeacherUserDTO.class,wrapper);
    }

}
